package com.xjgc.msi.exo.wsclient;

import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

import java.io.Serializable;

/**
 * WebService客户端 标准请求格式
 *
 * @author: 小逗号
 * @date: 2023-12-26
 * @description:
 * @since:
 */
@Slf4j
@Data
@NoArgsConstructor
public class WsClientStandardResponse implements Serializable {
    public final static String STATE_SUCCESS = "S";
    public final static String STATE_FAIL = "F";

    private String serialNo;
    private String state;
    private String message;
    private String extend;

    public WsClientStandardResponse(String params, Object[] objects) {
        params = StringUtils.isBlank(params) ? DEFAULT_PARAMS : params;
        String[] split = params.split(SEPARATOR);
        for (int i = 0; i < split.length; i++) {
            String param = split[i];
            String value = objects[i].toString();
            if (Param.SERIAL_NO.toString().equals(param)) {
                this.serialNo = value;
            } else if (Param.STATE.toString().equals(param)) {
                this.state = value;
            } else if (Param.MESSAGE.toString().equals(param)) {
                this.message = value;
            } else if (Param.EXTEND.toString().equals(param)) {
                this.extend = value;
            } else {
                log.warn("未知的参数配置!{}", param);
            }
        }
    }

    /**
     * 分隔符
     */
    public final static String SEPARATOR = ",";
    /**
     * 默认参数列表
     */
    public final static String DEFAULT_PARAMS = Param.SERIAL_NO + SEPARATOR + Param.STATE + SEPARATOR + Param.MESSAGE + SEPARATOR + Param.EXTEND;

    public enum Param {
        /*SERIAL_NO=流水号,STATE=状态,MESSAGE=消息,EXTEND预留*/
        SERIAL_NO,
        STATE,
        MESSAGE,
        EXTEND,
        ;

        public static int getIndex(String param) {
            if (SERIAL_NO.toString().equals(param)) {
                return 0;
            }
            if (STATE.toString().equals(param)) {
                return 1;
            }
            if (MESSAGE.toString().equals(param)) {
                return 2;
            }
            if (EXTEND.toString().equals(param)) {
                return 3;
            }
            return -1;
        }
    }
}
